package artibella.ui.combos 
{
	import artibella.IDisposable;
	import flash.display.Sprite;
	import caurina.transitions.properties.ColorShortcuts;
	import caurina.transitions.Tweener;
	import flash.events.Event;
	
		/**
	 * ...
	 * @author pbordachar
	 */
	
	public class DateComboSet extends Sprite implements IDisposable
	{
		private var _comboDD:Combo;
		private var _comboMM:Combo;
		private var _comboYYYY:Combo;
		//
		private var _defaultDD:String = "DD";
		private var _defaultMM:String = "MM";
		private var _defaultYYYY:String = "YYYY";
		
		private var _DD:String;
		private var _MM:String;
		private var _YYYY:String;
		
		public function DateComboSet() 
		{
			if ( stage ) {
				_init()
			} else {
				addEventListener( Event.ADDED_TO_STAGE, _init );
			}
		}
		
		private function _init( e:Event = null ):void
		{
			removeEventListener( Event.ADDED_TO_STAGE, _init );
			
			ColorShortcuts.init();
			
			_comboDD = _setCombo( 0, 45, _defaultDD, 1, 31 );
			_comboDD.addEventListener( ComboEvent.SELECTED, _onDDSelection );
			_comboDD.addEventListener( ComboEvent.OPENED, _onComboOpened );
			addChild( _comboDD );
			
			_comboMM = _setCombo( 55, 50, _defaultMM, 1, 12 );
			_comboMM.addEventListener( ComboEvent.SELECTED, _onMMSelection );
			_comboMM.addEventListener( ComboEvent.OPENED, _onComboOpened );
			addChild( _comboMM );
			
			_comboYYYY = _setCombo( 115, 60, _defaultYYYY, 1930, 1990 );
			_comboYYYY.addEventListener( ComboEvent.SELECTED, _onYYYYSelection );
			_comboYYYY.addEventListener( ComboEvent.OPENED, _onComboOpened );
			addChild( _comboYYYY );

			
		}
		
		private function _setCombo( posX:int, w:int, str:String, min:int, max:int ):Combo
		{
			var combo:Combo = new Combo();
			combo.x = posX;
			combo.y = 0;
			
			var bg:Sprite = new Sprite();
			bg.graphics.lineStyle( 1, 0, 1 );
			bg.graphics.beginFill( 0xFFFFFF );
			bg.graphics.drawRect( 0, 0, w, 20 );
			bg.graphics.endFill();
			combo.setHeaderBackground( bg );
			
			//var btn:Sprite = new Sprite();
			//btn.graphics.beginFill( 0xC6C4C7 );
			//btn.graphics.drawRect( 0, 0, 20, 20 );
			//btn.graphics.endFill();
			//var arrow:Sprite = new Sprite();
			//arrow.graphics.beginFill( 0x666666 );
			//arrow.graphics.moveTo( 6, 8 );
			//arrow.graphics.lineTo( 14, 8 );
			//arrow.graphics.lineTo( 10, 12 );
			//arrow.graphics.lineTo( 6, 8 );
			//arrow.graphics.endFill();
			//arrow.x = 10;
			//arrow.y = 10;
			//arrow.rotation = 90;
			//btn.addChild( arrow );
			//Tweener.addTween( arrow, { _color:0xFFFFFF } );
			//combo.setButton( btn );
			//
			//combo.setDefaultSelection( - 1, str );

			var item:ComboListItem;
			var text:String;
			for ( var i:int = min; i <= max; i ++ ) {
				item = new ComboListItem( i - 1 );
				text = ( i < 10 ) ? "0" + i.toString() : i.toString() ; 
				item.setText( text );
				combo.addItem( item );
			}
			
			combo.finalize( stage, 5 );
			
			return combo;
		}
		
		private function _onDDSelection( e:ComboEvent ):void
		{
			_DD = e.text;
		}
		
		private function _onMMSelection( e:ComboEvent ):void
		{
			_MM = e.text;
		}
		
		private function _onYYYYSelection( e:ComboEvent ):void
		{
			_YYYY = e.text;
		}
		
		private function _onComboOpened( e:ComboEvent ):void
		{
			switch( e.text ) {
				case _defaultDD : 
					_comboMM.doClose();
					_comboYYYY.doClose();
					break;
				case _defaultMM : 
					_comboDD.doClose();
					_comboYYYY.doClose();
					break;
				case _defaultYYYY: 
					_comboDD.doClose();
					_comboMM.doClose();
					break;
			}
		}
		
		// - - -
		
		public function set defaultDD( s:String ):void
		{
			_defaultDD = s;
		}
		
		public function set defaultMM( s:String ):void
		{
			_defaultMM = s;
		}
		
		public function set defaultYYYY( s:String ):void
		{
			_defaultYYYY = s;
		}
		
		// - - -
		
		public function get DD():String
		{
			return _DD;
		}
		
		public function get MM():String
		{
			return _MM;
		}
		
		public function get YYYY():String
		{
			return _YYYY;
		}
		
		
		// - - - IDisposable
		
		public function dispose():void
		{
			
		}
	}
}